﻿using System;
using System.Threading;
using OpcUaHelper;


namespace OpcUaHelper.NetCoreDemo
{
    public class Program
    {
        //static OpcUaClient opcUaClient = new OpcUaClient();

        //public Program()
        //{
        //    try
        //    {
        //        opcUaClient.ConnectServer("opc.tcp://127.0.0.1:49320");
        //    }
        //    catch (Exception ex)
        //    {
        //        Console.WriteLine("Connect failed : " + ex.Message);
        //    }
        //}

        //~Program()
        //{
        //    opcUaClient.Disconnect();
        //}


        static void Main(string[] args)
        {
            GetValues();
        }

        public static void GetValues()
        {
            OpcUaClient opcUaClient = new OpcUaClient();

            try
            {
                bool value = opcUaClient.ReadNode<bool>("ns=2;s=Machines/Machine B/TestValueFloat");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

        }

        /// <summary>
        /// 自动模式-气缸一开启
        /// </summary>
        public static void PutpoolopenOneN()
        {
            OpcUaClient opcUaClient = new OpcUaClient();

            try
            {
                opcUaClient.ConnectServer("opc.tcp://127.0.0.1:49320");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Connect failed : " + ex.Message);
            }

            opcUaClient.WriteNode("ns=2;s=AB800.Micro850.E02", (bool)true);
            Thread.Sleep(500);
            opcUaClient.WriteNode("ns=2;s=AB800.Micro850.E02", (bool)false);

            opcUaClient.Disconnect();
        }

        /// <summary>
        /// 自动模式-气缸一关闭
        /// </summary>
        public static void PutpoolcloseOneN()
        {
            OpcUaClient opcUaClient = new OpcUaClient();

            try
            {
                opcUaClient.ConnectServer("opc.tcp://127.0.0.1:49320");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Connect failed : " + ex.Message);
            }

            opcUaClient.WriteNode("ns=2;s=AB800.Micro850.E11", (bool)true);
            Thread.Sleep(500);
            opcUaClient.WriteNode("ns=2;s=AB800.Micro850.E11", (bool)false);

            opcUaClient.Disconnect();
        }

        /// <summary>
        /// 自动模式-气缸二开启
        /// </summary>
        public static void PutpoolopenTwoN()
        {
            OpcUaClient opcUaClient = new OpcUaClient();

            try
            {
                opcUaClient.ConnectServer("opc.tcp://127.0.0.1:49320");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Connect failed : " + ex.Message);
            }

            opcUaClient.WriteNode("ns=2;s=AB800.Micro850.E03", (bool)true);
            Thread.Sleep(500);
            opcUaClient.WriteNode("ns=2;s=AB800.Micro850.E03", (bool)false);

            opcUaClient.Disconnect();
        }

        /// <summary>
        /// 自动模式-气缸二关闭
        /// </summary>
        public static void PutpoolcloseTwoN()
        {
            OpcUaClient opcUaClient = new OpcUaClient();

            try
            {
                opcUaClient.ConnectServer("opc.tcp://127.0.0.1:49320");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Connect failed : " + ex.Message);
            }

            opcUaClient.WriteNode("ns=2;s=AB800.Micro850.E13", (bool)true);
            Thread.Sleep(500);
            opcUaClient.WriteNode("ns=2;s=AB800.Micro850.E13", (bool)false);


            opcUaClient.Disconnect();
        }

        /// <summary>
        /// 计数器复位按钮
        /// </summary>
        public static void Reset()
        {
            OpcUaClient opcUaClient = new OpcUaClient();

            try
            {
                opcUaClient.ConnectServer("opc.tcp://127.0.0.1:49320");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Connect failed : " + ex.Message);
            }

            opcUaClient.WriteNode("ns=2;s=AB800.Micro850.E15", (bool)true);
            Thread.Sleep(500);
            opcUaClient.WriteNode("ns=2;s=AB800.Micro850.E15", (bool)false);

            opcUaClient.Disconnect();
        }
        /// <summary>
        /// 切换模式
        /// </summary>
        /// <param name="b">1：手动模式；0：自动模式</param>
        public static void SetModel(bool b)
        {

            OpcUaClient opcUaClient = new OpcUaClient();

            try
            {
                opcUaClient.ConnectServer("opc.tcp://127.0.0.1:49320");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Connect failed : " + ex.Message);
            }

            if (b.Equals(true))
            {
                opcUaClient.WriteNode("ns=2;s=AB800.Micro850.E01", (bool)true);
            }
            else
            {
                opcUaClient.WriteNode("ns=2;s=AB800.Micro850.E01", (bool)false);
            }

            opcUaClient.Disconnect();
        }
        /// <summary>
        /// 获取当前模式
        /// </summary>
        /// <returns></returns>
        public static bool GetModel() {
            OpcUaClient opcUaClient = new OpcUaClient();

            try
            {
                opcUaClient.ConnectServer("opc.tcp://127.0.0.1:49320");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Connect failed : " + ex.Message);
            }

            bool b = false; 
            
            try
            {
                 b = opcUaClient.ReadNode<bool>("ns=2;s=AB800.Micro850.E01");
            }
            catch (Exception)
            {

                
            }


            opcUaClient.Disconnect();
                  
            return b;
        }
        /// <summary>
        /// 获取摄像头一的状态
        /// </summary>
        /// <returns></returns>
        public static bool GetCameraOneStatus()
        {
            OpcUaClient opcUaClient = new OpcUaClient();

            try
            {
                opcUaClient.ConnectServer("opc.tcp://127.0.0.1:49320");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Connect failed : " + ex.Message);
            }

            bool b = false;

            try
            {
                b = opcUaClient.ReadNode<bool>("ns=2;s=AB800.Micro850.E06");
            }
            catch 
            {

                b = false;
            }

            opcUaClient.Disconnect();

            return b;
        }

        /// <summary>
        /// 获取摄像头二的状态
        /// </summary>
        /// <returns></returns>
        public static bool GetCameraTwoStatus()
        {
            OpcUaClient opcUaClient = new OpcUaClient();

            try
            {
                opcUaClient.ConnectServer("opc.tcp://127.0.0.1:49320");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Connect failed : " + ex.Message);
            }

            bool b = false;
            try
            {
                b = opcUaClient.ReadNode<bool>("ns=2;s=AB800.Micro850.E05");
            }
            catch 
            {

            }


            opcUaClient.Disconnect();

            return b;
        }

        /// <summary>
        /// 获取计数
        /// </summary>
        /// <returns></returns>
        public static Int32 GetIndex()
        {
            OpcUaClient opcUaClient = new OpcUaClient();

            try
            {
                opcUaClient.ConnectServer("opc.tcp://127.0.0.1:49320");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Connect failed : " + ex.Message);
            }

            Int32 b = opcUaClient.ReadNode<Int32>("ns=2;s=AB800.Micro850.E04");

            opcUaClient.Disconnect();

            return b;
        }

    }
}
